//
//  OrderCell.swift
//  vmes
//
//  Created by user on 2021/10/26.
//

import UIKit
import SnapKit

class OrderCell: UITableViewCell{
    
    let nameLabel=UILabel()   // 名字
    let selectOrder=UILabel()    //选取次序
    
    let descLabel=UILabel()    // 描述
    let typeLabel=UILabel()
    let classLabel=UILabel()   //班次
    let productLabel=UILabel()    // 描述
    let timeLabel=UILabel()       // 时间
    let moreLabel=UILabel()       // 时间
    
    let materialLabel=UILabel()   //物料材质
    let materialColorLabel=UILabel()    //物料颜色
    
    let planNumLb=UILabel()
    let autoNumLb=UILabel()
    let allNumLb=UILabel()
    
    var iconStatus:UIImageView!    // 状态
    var image:UIImageView! //同步开工icon
    
    var orderData:OrderItem?
    
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //        self.selectionStyle = .gray
        let layout=UIView()
        layout.backgroundColor = UIColor.colorWithHex(hexStr: "#E5EBF2")
        contentView.addSubview(layout)
        layout.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(5)
            make.bottom.equalToSuperview().offset(-5)
        }
        image = UIImageView(image: UIImage(named: "icon_batch"))
        layout.addSubview(image)
        nameLabel.textColor=UIColor.colorWithHex(hexStr: "#333333")
        nameLabel.font = .systemFont(ofSize: 22)
        layout.addSubview(nameLabel)
        
        [selectOrder,descLabel,productLabel,timeLabel,materialLabel,materialColorLabel
         ,planNumLb,autoNumLb,allNumLb,classLabel,typeLabel,moreLabel].forEach({
            $0.textColor=UIColor.colorWithHex(hexStr: "#333333",alpha: 0.8)
            $0.font = .systemFont(ofSize: 13)
            layout.addSubview($0)
        })
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(10)
        }
        descLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(nameLabel.snp.bottom).offset(2)
        }
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(descLabel.snp.right).offset(5)
            make.top.equalTo(descLabel.snp.top)
        }
        classLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(descLabel.snp.top)
        }
        classLabel.textColor = .red
        productLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(descLabel.snp.bottom).offset(2)
        }
        materialLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(productLabel.snp.bottom).offset(2)
        }
        materialColorLabel.snp.makeConstraints { make in
            make.left.equalTo(materialLabel.snp.right).offset(10)
            make.top.equalTo(materialLabel.snp.top)
        }
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(materialLabel.snp.bottom).offset(2)
        }
        
        allNumLb.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalTo(productLabel.snp.bottom)
        }
        
        autoNumLb.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(timeLabel.snp.top)
        }
        
        planNumLb.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalTo(materialLabel.snp.bottom)
        }
        
        image.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.left.equalTo(nameLabel.snp.right).offset(10)
        }
        
        
        
        selectOrder.snp.makeConstraints { make in
            make.centerY.equalTo(image.snp.centerY)
            make.left.equalTo(image.snp.right).offset(3)
        }
        iconStatus=UIImageView(image: UIImage(named: "icon_wait_process"))
        layout.addSubview(iconStatus)
        iconStatus.snp.makeConstraints { make in
            make.right.top.equalToSuperview()
        }
        
        moreLabel.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.right.equalTo(iconStatus.snp.left).offset(10)
        }
        moreLabel.text="更多"
        image.isHidden=true
        // 初始化一个UITapGestureRecognizer对象
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showMoreAlert))
        // 设置手势识别器的代理，以便能够识别UILabel的子视图
        // 将手势识别器添加到UILabel上
        moreLabel.addGestureRecognizer(tapGestureRecognizer)
        // 设置UILabel的用户交互为开启，这样它才能接收手势
        moreLabel.isUserInteractionEnabled = true
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    @objc func showMoreAlert(){
        let tb=orderData?.tbCCustomers
        let mutableString = NSMutableString(string: "客户名称：\(tb?.clientInfo ?? "暂无内容")")
        mutableString.append("\n订单号：\(tb?.orderNum ?? "暂无内容")")
        mutableString.append("\n型号规格：\(tb?.typeSpecification ?? "暂无内容")")
        mutableString.append("\n产品名称：\(tb?.productName ?? "暂无内容")")
        mutableString.append("\n数量：\(tb?.planOutput ?? "暂无内容")")
        mutableString.append("\n生产部门：\(tb?.productionDepartment ?? "暂无内容")")
        mutableString.append("\n铁芯长度：\(String(describing:tb?.coreLength ?? 0))")
        mutableString.append("\n计量单位：\(tb?.measuringUnit ?? "暂无内容")")
        mutableString.append("\n技术标准：\(tb?.technicalStandard ?? "暂无内容")")
        mutableString.append("\n客户参数：\(tb?.customParameter ?? "暂无内容")")
        let alertView = UIAlertController.init(title: "更多内容", message: mutableString as String, preferredStyle: .alert)
        let cancleAlert = UIAlertAction.init(title: "确定", style: .cancel,handler: nil)
        alertView.addAction(cancleAlert)
        if let presentingVC = findViewController() {
            presentingVC.present(alertView, animated: true, completion: nil)
        }
    }
    
    //    func getUpVC() -> UIViewController? {
    //        let rootVC = UIApplication.shared.keyWindow?.rootViewController
    //        let currentVC = getCurrentVC(from: rootVC)
    //        return currentVC
    //    }
    //
    //    func getCurrentVC(from rootVC: UIViewController?) -> UIViewController? {
    //        var rootVC = rootVC
    //        var currentVC: UIViewController?
    //        if rootVC?.presentedViewController != nil {
    //            // 视图是被presented出来的
    //            rootVC = rootVC?.presentedViewController
    //        }
    //        if rootVC is UITabBarController {
    //            let tabBar = rootVC as! UITabBarController
    //            currentVC = getCurrentVC(from: tabBar.selectedViewController)
    //        } else if rootVC is UINavigationController {
    //            let nav = rootVC as! UINavigationController
    //            currentVC = getCurrentVC(from: nav.visibleViewController)
    //        } else {
    //            currentVC = rootVC
    //        }
    //        return currentVC
    //    }
    
    func findViewController() -> UIViewController? {
        var nextResponder: UIResponder? = self
        while let responder = nextResponder {
            nextResponder = responder.next
            if let viewController = responder as? UIViewController {
                return viewController
            }
        }
        return nil
    }
    
}
